/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package action.product;

import static com.opensymphony.xwork2.Action.INPUT;
import static com.opensymphony.xwork2.Action.SUCCESS;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import database.ProductDAO;
import database.ProductManagerDAO;
import database.StaffAuthenticationDAO;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import model.Category;
import model.Product;
import model.Staff;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;

/**
 *
 * @author Trang
 */
public class AddProductAction extends ActionSupport implements ModelDriven<Product>{
    
    private Product product = new Product();
    private List<Product> products = new ArrayList<Product>();
    private String imageurl;
    private Map<Integer,String> sorts;
    private Map<Integer,String> allCategory;
    private Integer idCategory;
    private List<Category> categories;
    public ProductManagerDAO productDAO = new ProductManagerDAO();
    public ProductDAO productDao = new ProductDAO();

    public Product getProduct() {
        return product;
    }

    public List<Product> getProducts() {
        return products;
    }

    public void setProducts(List<Product> products) {
        this.products = products;
    }
    
    
    public void setProduct(Product product) {
        this.product = product;
    }

    public String getImageurl() {
        return imageurl;
    }

    public void setImageurl(String imageurl) {
        this.imageurl = imageurl;
    }

    public Map<Integer, String> getSorts() {
        return sorts;
    }

    public void setSorts(Map<Integer, String> sorts) {
        this.sorts = sorts;
    }

    public Map<Integer, String> getAllCategory() {
        return allCategory;
    }

    public void setAllCategory(Map<Integer, String> allCategory) {
        this.allCategory = allCategory;
    }

    public Integer getIdCategory() {
        return idCategory;
    }

    public void setIdCategory(Integer idCategory) {
        this.idCategory = idCategory;
    }

    

    public List<Category> getCategories() {
        return categories;
    }

    public void setCategories(List<Category> categories) {
        this.categories = categories;
    }
    
    
     @Action(value = "addProduct", results = {
        @Result(name = "success", location = "/manageProduct/w_product.jsp"),
        @Result(name = "input", location = "/manageProduct/w_addproduct.jsp")
    })
    public String execute() throws Exception {
        String result;
        Category cate = new Category();
        Map session = ActionContext.getContext().getSession();
        imageurl = (String) session.get("currentImage");
        
        cate =  productDAO.getCategoryFromId(idCategory);
        
        System.out.println("trang"+cate);
        product.setCategory(cate);
        product.setImageurl("image/"+getImageurl());
        product.setCode("PM"+productDAO.getLastidProduct()+"");
        System.out.println(product);
        if (productDao.addProduct(product)) {
            addActionMessage("Add product Successfully!");
            Staff staff = (Staff) session.get("staff");
            new StaffAuthenticationDAO().addAction("add product", staff.getIdStaff());
                    
            sorts = new HashMap<Integer, String>();
            sorts.put(1, "Product Code");
            sorts.put(2, "Product Name");
            sorts.put(3, "Unit Price");
            sorts.put(4, "Stock Quantity");
            
            products = productDAO.GetAllProduct();
            result = SUCCESS;
        } else {
            addActionError("Upload error (due to existed product name).");
            allCategory = new HashMap<Integer, String>();
            categories = productDAO.getChildrenCategory();
            for(Category item: categories)
               {
                   allCategory.put(item.getIdCategory(), item.getName());
               } 
            result = INPUT;
        }
        return result;
    }

    @Override
    public Product getModel() {
        return product; //To change body of generated methods, choose Tools | Templates.
    }
}
